package com.ease.plugin.loaderClasses {
  import com.ease.core.plugin.ResourceBase;
  import com.ease.plugin.traits.LoadState;
  import com.ease.plugin.traits.LoadTrait;
  import com.ease.plugin.traits.LoaderBase;

  import flash.display.Loader;
  import flash.events.ProgressEvent;

  /**
   * @private
   **/
  public class LoaderLoadTrait extends LoadTrait {
    public function LoaderLoadTrait(loader:LoaderBase, resource:ResourceBase) {
      super(loader, resource);
    }

    public function get loader():Loader {
      return _loader;
    }

    public function set loader(value:Loader):void {
      _loader = value;
    }

    override protected function loadStateChangeStart(newState:String):void {
      if (newState == LoadState.READY) {
        // Update to current values.
        setBytesTotal(loader.contentLoaderInfo.bytesTotal);
        setBytesLoaded(loader.contentLoaderInfo.bytesLoaded);

        // But listen for any changes.
        loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onContentLoadProgress, false, 0, true);
      }
      else if (newState == LoadState.UNINITIALIZED) {
        setBytesLoaded(0);
      }
    }

    // Internals
    //

    private function onContentLoadProgress(event:ProgressEvent):void {
      setBytesTotal(event.bytesTotal);
      setBytesLoaded(event.bytesLoaded);
    }

    private var _loader:Loader;
  }
}